import config from "./config.js"

const request = {};

request.get = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '加载中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'GET',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				uni.hideLoading();
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.getNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'GET',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				uni.hideLoading();
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.post = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '加载中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'POST',
			data: params,
			header: headers,
			dataType: 'json',
			success: (res) => {
				uni.hideLoading();
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.postNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'POST',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.showToast({
					title: '网络不给力，请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.put = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '加载中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'PUT',
			data: params,
			header: headers,
			dataType: 'json',
			success: (res) => {
				console.log(url);
				console.log(res.data);
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.putNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'PUT',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.showToast({
					title: '网络不给力，请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.delete = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '加载中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'DELETE',
			data: params,
			header: headers,
			dataType: 'json',
			success: (res) => {
				console.log(url);
				console.log(res.data);
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.deleteNoLoading = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method: 'DELETE',
			data: params,
			dataType: 'json',
			header: headers,
			success: (res) => {
				console.log(url);
				console.log(res.data);
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				uni.showToast({
					title: '网络不给力，请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.uploadFile = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '上传中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + url,
			filePath: params.file,
			header: headers,
			name: 'files',
			success: (res) => {
				res.data = JSON.parse(res.data);
				uni.hideLoading();
				if (res.data.code === 0) {
					return resolve(res.data);
				} else if (res.data.code === 401) {
					uni.redirectTo({
						url: "/pages/oneKeyLogin"
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				console.log(error);
				uni.hideLoading();
				uni.showToast({
					title: '网络不给力,请稍后重试',
					icon: 'none'
				})
			}
		})
	});
}

request.uploadFiles = (url, params) => {
	let headers = {};
	const token = uni.getStorageSync('token');
	if (token) {
		headers['x-token'] = token;
	}
	uni.showLoading({
		title: '上传中...',
		mask: true
	});
	return new Promise((resolve, reject) => {
		let index = 0;
		const data = [];
		params.files.forEach(file => {
			uni.uploadFile({
				url: config.baseUrl + url,
				filePath: file,
				header: headers,
				name: 'files',
				success: (res) => {
					uni.hideLoading();
					console.log(url);
					console.log(res.data);
					if (res.statusCode === 200) {
						res.data = JSON.parse(res.data);
						if (res.data.code === 0) {
							index++;
							data.push(res.data.data[0]);
							if (index === params.files.length) {
								res.data.data = data;
								return resolve(res.data);
							}
						} else if (res.data.code === 401) {
							uni.redirectTo({
								url: "/pages/oneKeyLogin"
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '网络不给力,请稍后重试',
							icon: 'none'
						})
					}
				},
				fail: (error) => {
					uni.hideLoading();
					uni.showToast({
						title: '网络不给力,请稍后重试',
						icon: 'none'
					})
				}
			})
		})
	});
}

export default request;
